package com.beagle.njddapp.ui.presenter;

import com.beagle.njddapp.base.ApiService;
import com.beagle.njddapp.base.BasePresenter;
import com.beagle.njddapp.base.NjOwnerApiService;
import com.beagle.njddapp.base.OilApiService;
import com.beagle.njddapp.base.RetrofitHttp;
import com.beagle.njddapp.bean.ZdBean;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.bean.owner.OilOrderBean;
import com.beagle.njddapp.core.UserService;
import com.beagle.njddapp.net.BaseOilHttpListResponse;
import com.beagle.njddapp.net.BaseZdHttpListResponse;
import com.beagle.njddapp.net.HttpResponseObserver;
import com.beagle.njddapp.net.RxSchedulers;
import com.beagle.njddapp.ui.view.SuggestView;
import com.beagle.njddapp.ui.view.WalletView;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.LinkedHashMap;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

/**
 * @author hxx
 * @date 2021-03-02 22:33
 */
public class SuggestPresenter extends BasePresenter<SuggestView> {
    private NjOwnerApiService njOwnerApiService;
    private OilApiService OilapiService;
    private ApiService apiService;

    public SuggestPresenter() {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        apiService = retrofit.create(ApiService.class);
        njOwnerApiService = retrofit.create(NjOwnerApiService.class);
        OilapiService = retrofit.create(OilApiService.class);
        apiService = retrofit.create(ApiService.class);

    }


    public void fuelOrders(int size,int page){
        Gson gson=new Gson();
        HashMap<String,String> paramsMap=new LinkedHashMap<>();
        paramsMap.put("orderState","all");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        paramsMap.clear();
        njOwnerApiService.fuelOrders(size+"",page+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<OilOrderBean>>() {
            @Override
            public void success(BaseOilHttpListResponse<OilOrderBean> orderBeanBaseOilHttpListResponse) {
                if (orderBeanBaseOilHttpListResponse.results!=null){
//                    getView().showOrders(orderBeanBaseOilHttpListResponse.results);
                }else {
                    getView().onError("");
                }
            }

            @Override
            public void error(int code, String message) {
                getView().onError(message);
            }
        });
    }


    public void getList() {
        if (UserService.getInstance().getRole()==UserService.ROLE_OWNER){
            Gson gson=new Gson();
            HashMap<String,String> paramsMap=new LinkedHashMap<>();
            paramsMap.put("orderState","all");
            String strEntity = gson.toJson(paramsMap);
            RequestBody body =RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
            paramsMap.clear();
            njOwnerApiService.fuelOrders(1000+"",1+"",body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseOilHttpListResponse<OilOrderBean>>() {
                @Override
                public void success(BaseOilHttpListResponse<OilOrderBean> orderBeanBaseOilHttpListResponse) {
                    if (orderBeanBaseOilHttpListResponse.results!=null){
                     getView().showOrders(orderBeanBaseOilHttpListResponse.results);
                    }else {
                        getView().onError("");
                    }
                }

                @Override
                public void error(int code, String message) {
                    getView().onError(message);
                }
            });
        }else if (UserService.getInstance().getRole()==UserService.ROLE_OIL){
            Gson gson = new Gson();
            HashMap<String, String> paramsMap = new LinkedHashMap<>();
            paramsMap.put("orderState", "all");
            String strEntity = gson.toJson(paramsMap);
            RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
            paramsMap.clear();
            OilapiService.getOrderList(1000+"",1+"",body).compose(RxSchedulers.compose()).safeSubscribe(new HttpResponseObserver<BaseOilHttpListResponse<OilHomeBean>>() {
                @Override
                public void success(BaseOilHttpListResponse<OilHomeBean> oilHomeBeanBaseOilHttpResponse) {
                    if (oilHomeBeanBaseOilHttpResponse!=null){
                    getView().showOrderList(oilHomeBeanBaseOilHttpResponse.results);
                    }else {
                        getView().onError("");

                    }
                }

                @Override
                public void error(int code, String message) {
                    getView().onError(message);
                }
            });
        }

    }

    public void queryDictList() {
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new LinkedHashMap<>();
        paramsMap.put("dictName", "意见反馈类型");
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        apiService.queryDictListByName(body).compose(RxSchedulers.compose()).subscribe(new HttpResponseObserver<BaseZdHttpListResponse<ZdBean>>() {


            @Override
            public void success(BaseZdHttpListResponse<ZdBean> zdHttpListResponse) {
                LogUtils.d("success");
                if (zdHttpListResponse.items!=null){
                    getView().showZd(zdHttpListResponse.items);
                }else {
                    getView().onError("");

                }
            }

            @Override
            public void error(int code, String message) {
                LogUtils.d("error");
                getView().onError(message);
            }
        });

    }
    public void commit(String content,String type,String orderNUm) {
        retrofit2.Retrofit retrofit = RetrofitHttp.provideRetrofit();
        //获取接口对象
        ApiService apiService = retrofit.create(ApiService.class);
        Gson gson = new Gson();
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("content", content);
        paramsMap.put("orderNum", orderNUm);
        paramsMap.put("type", type);
        String strEntity = gson.toJson(paramsMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        paramsMap.clear();
        apiService.feedback(body).compose(RxSchedulers.compose()).subscribe(new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object o) {
                getView().showSuccess();
            }

            @Override
            public void onError(Throwable e) {
                getView().showSuccess();
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
